package de.stefanocke.roo.kontaktliste.web.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.form.Form;

/**
 * A button for an action. Typically (?) has an icon. Optionally has text.
 * <p>
 * This class is specific to Twitter Bootstrap. See http://twitter.github.com/bootstrap/base-css.html#icons
 *  
 *
 */
public abstract class IconButton extends AjaxFallbackButton {
	
	public static final String DELETE = "delete";
	public static final String EDIT = "edit";
	public static final String SHOW = "show";
	public static final String NEW = "new";
	public static final String SAVE = "save";
	
	protected static final Map<String, String> KIND_TO_ICON_CSSCLASS = new HashMap<String, String>();
	
	static {
		KIND_TO_ICON_CSSCLASS.put(DELETE, "icon-minus-sign icon-white");
		KIND_TO_ICON_CSSCLASS.put(EDIT, "icon-edit icon-white");
		KIND_TO_ICON_CSSCLASS.put(SHOW, "icon-info-sign");
		KIND_TO_ICON_CSSCLASS.put(NEW, "icon-plus-sign icon-white");
		KIND_TO_ICON_CSSCLASS.put(SAVE, "icon-ok-sign icon-white");
		
	}
	
	protected static final Map<String, String> KIND_TO_BTN_CSSCLASS = new HashMap<String, String>();
	
	static {
		KIND_TO_BTN_CSSCLASS.put(DELETE, "btn-danger");
		KIND_TO_BTN_CSSCLASS.put(EDIT, "btn-info");
		KIND_TO_BTN_CSSCLASS.put(NEW, "btn-info");
	}
	
	
	
	public IconButton(String id, Form<?> form) {
		super(id, form);
		if(getBtnCssClass()!=null){
			add(new ConditionalCssAttributeBehavior() {		
				@Override
				protected String getCssClass() {
					return getBtnCssClass();
				}
			
			});
		}
	}
	
	@Override
	public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
		getResponse().write("<i class=\"");
		getResponse().write(getIconCssClass());
		getResponse().write("\"></i>");
		String text = getText();
		if(text!=null){
			getResponse().write(" ");
			getResponse().write(text);
		}
		super.onComponentTagBody(markupStream, openTag);
	}
	
	protected String getText(){
		return getLocalizer().getStringIgnoreSettings("action."+getKind(), this, null, null);
	}

	protected String getBtnCssClass() {
		return KIND_TO_BTN_CSSCLASS.get(getKind());
	}

	protected String getIconCssClass(){
		String cssClass = KIND_TO_ICON_CSSCLASS.get(getKind());
		if(cssClass==null){
			throw new IllegalArgumentException("Unknown button kind:"+getKind());
		}
		return cssClass;
	}
	/**
	 * 
	 * @return the kind of button to render. 
	 */
	protected abstract String getKind();

	
	

}
